package com.raigor.application.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.raigor.common.annotation.Log;
import com.raigor.common.core.controller.BaseController;
import com.raigor.common.core.domain.AjaxResult;
import com.raigor.common.enums.BusinessType;
import com.raigor.application.domain.TestScript;
import com.raigor.application.service.ITestScriptService;
import com.raigor.common.utils.poi.ExcelUtil;
import com.raigor.common.core.page.TableDataInfo;

/**
 * 测试脚本Controller
 *
 * @author ruoyi
 * @date 2021-12-21
 */
@RestController
@RequestMapping("/system/script")
public class TestScriptController extends BaseController
{
    @Autowired
    private ITestScriptService testScriptService;

    /**
     * 查询测试脚本列表
     */
    @PreAuthorize("@ss.hasPermi('system:script:list')")
    @GetMapping("/list")
    public TableDataInfo list(TestScript testScript)
    {
        startPage();
        List<TestScript> list = testScriptService.selectTestScriptList(testScript);
        return getDataTable(list);
    }

    /**
     * 导出测试脚本列表
     */
    @PreAuthorize("@ss.hasPermi('system:script:export')")
    @Log(title = "测试脚本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TestScript testScript)
    {
        List<TestScript> list = testScriptService.selectTestScriptList(testScript);
        ExcelUtil<TestScript> util = new ExcelUtil<TestScript>(TestScript.class);
        util.exportExcel(response, list, "测试脚本数据");
    }

    /**
     * 获取测试脚本详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:script:query')")
    @GetMapping(value = "/{scriptId}")
    public AjaxResult getInfo(@PathVariable("scriptId") Long scriptId)
    {
        return AjaxResult.success(testScriptService.selectTestScriptByScriptId(scriptId));
    }

    /**
     * 新增测试脚本
     */
    @PreAuthorize("@ss.hasPermi('system:script:add')")
    @Log(title = "测试脚本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TestScript testScript)
    {
        return toAjax(testScriptService.insertTestScript(testScript));
    }

    /**
     * 修改测试脚本
     */
    @PreAuthorize("@ss.hasPermi('system:script:edit')")
    @Log(title = "测试脚本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TestScript testScript)
    {
        return toAjax(testScriptService.updateTestScript(testScript));
    }

    /**
     * 删除测试脚本
     */
    @PreAuthorize("@ss.hasPermi('system:script:remove')")
    @Log(title = "测试脚本", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scriptIds}")
    public AjaxResult remove(@PathVariable Long[] scriptIds)
    {
        return toAjax(testScriptService.deleteTestScriptByScriptIds(scriptIds));
    }
}
